<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/5/9
 * Time: 10:53
 */
namespace app\home\controller;
use think\Db;
use think\Request;
class User extends Base {
    //修改资料
    public function index(){
        if(request()->isPost()){
            extract(input());
            $username = input('param.user_username');
            $task_image = input('param.task_image');
            $data['user_image'] = $task_image;
            $data['user_username'] = $username;
            if(isset($user_year_3) && $user_year_3){
                $data['user_year_3'] = $user_year_3;
            }
            if(isset($user_content_3) && $user_content_3){
                $data['user_content_3'] = $user_content_3;
            }
            $data['user_update_time'] = date('Y-m-d H:i:s');
            $res1 =  Db::table('up_u_user')->where('user_id',session('user_id'))->update($data);
            if($res1){
                session('user_username',$username);//用户名称
                session('user_image',$task_image);//用户头像
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $res = Db::table('up_u_user')->where('user_id',session('user_id'))->find();
        $request= Request::instance();
        $controller_name=$request->controller();
        $action=$request->action();
        $aa = $controller_name.'/'.$action;
        $this->assign('aa',$aa);
        $this->assign('res',$res);
        $this->assign('title','设置');
        return $this->fetch();
    }
    //修改资料
    public function usersign(){
        if(request()->isPost()){
            extract(input());
            $ss = trim(input('param.ss'),',');
            $user =  Db::table('up_u_user')->where('user_id',session('user_id'))->find();
            if($user['user_signs_3']){
                $ss = $user['user_signs_3'] . ',' . $ss;
            }
            $res1 =  Db::table('up_u_user')->where('user_id',session('user_id'))->update([
                'user_signs_3' => $ss,
                'user_update_time' => date('Y-m-d H:i:s'),
            ]);
            if($res1){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $res = Db::table('up_u_user')->where('user_id',session('user_id'))->find();
        if($res['user_signs_3']){
            $res['user_signs_3'] = explode(',',$res['user_signs_3']);
        }else{
            $res['user_signs_3'] = [];
        }
        $request= Request::instance();
        $controller_name=$request->controller();
        $action=$request->action();
        $aa = $controller_name.'/'.$action;
        $this->assign('aa',$aa);
        $this->assign('res',$res);
        $this->assign('title','设置');
        return $this->fetch();
    }
    //关于我们
    public function about(){
        $data = Db::table('up_about')
            ->where('id',2)
            ->find();
        $request= Request::instance();
        $controller_name=$request->controller();
        $action=$request->action();
        $aa = $controller_name.'/'.$action;
        $this->assign('aa',$aa);
        $this->assign('data',$data);
        $this->assign('title','设置');
        $res = Db::table('up_u_user')->where('user_id',session('user_id'))->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
    //客服
    public function service(){
        if(request()->isPost()){
            $data['k_title'] = trim(input('param.title'),' ');
            $data['k_desc'] = trim(input('param.k_desc'),' ');
            $data['k_user_id'] = session('user_id');
            $data['k_time'] = date('Y-m-d H:i:s',time());
            if(!$data['k_title'] || !$data['k_desc']){
                return json(['code'=>100,'msg'=>'标题或者内容不能为空！']);
            }
            $res = Db::table('up_kefu')->insertGetId($data);
            if($res){
                return json(['code'=>200,'msg'=>'发布成功']);
            }else{
                return json(['code'=>100,'msg'=>'发布失败']);
            }
        }
        $this->assign('title','客服');
        return $this->fetch();
    }

    //收藏
    public function collect(){
        $data = Db::name('like')->alias('l')
            ->join('up_house uh','uh.h_id = l.like_h_id','left')
            ->join('up_u_user uuu','uuu.user_id = uh.h_user_id','left')
            ->field('uh.*,uuu.user_phone')
            ->order('l.like_id desc')
            ->where('like_user_id',session('user_id'))
            ->where('h_status',1)
            ->where('h_id','>',0)
            ->paginate(10);
         $page = $data->render();
         $data = $data->all();
        foreach ($data as $key=>&$vo){
            $sign = trim($vo['h_sign'],',');
            $where['id'] = array('IN',$sign);
            $name = Db::table('up_sign')->where($where)->column('name');
            $vo['sign_name'] = $name;
            $vo['price'] = floor($vo['h_price']);
        }
//        print_r($data);

        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('title','我的收藏');
        return $this->fetch();
    }

    //发布
    public function release(){
        $user_type =  Db::table('up_u_user')->where('user_id',session('user_id'))->value('user_type');
        if($user_type == 3){
            $data = Db::table('up_company_case')
                ->where('cc_u_id',session('user_id'))
                ->order('cc_time DESC')
                ->field('cc_id,cc_title,cc_style,cc_name,cc_thumb')
                ->paginate(10);
            $page = $data->render();
            $data = $data->all();
        }else{
            $userid = session('user_id');
            $data = Db::name('house')->alias('uh')
                ->join('up_u_user uuu','uuu.user_id = uh.h_user_id','left')
                ->join ('up_area a','uh.h_shi = a.district_id','LEFT')
                ->field('uh.*,uuu.user_phone,a.district')
                ->where (function ($query) use ($userid){
                    $query->where('h_user_id',$userid)->where('h_status',1);
                })
                ->whereOr (function ($query) use ($userid){
                    $query->where('h_user_id',$userid)->where('h_done',2);
                })
                ->order('uh.h_id desc')
                ->paginate(10);
            $page = $data->render();
            $data = $data->all();
            foreach ($data as $key=>&$vo){
                $sign = trim($vo['h_sign'],',');
                $where['id'] = array('IN',$sign);
                $name = Db::table('up_sign')->where($where)->column('name');
                $vo['sign_name'] = $name;
                $vo['price'] = floor($vo['h_price']);
            }
        }


        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('user_type',$user_type);
        $this->assign('title','我的发布');
        return $this->fetch();
    }
    /**
     * 我发布的房源之确定已完成
     */
    public function doneMyHouse()
    {
        extract(input());
        if(empty($h_id)){
            return json(['status'=>100,'msg'=>'请选择房源']);
        }
        $have = Db::table('up_house')
            ->where('h_id',$h_id)
            ->field('h_done,h_user_id')
            ->find();
        if($have['h_done'] == 2){
            return json(['status'=>100,'msg'=>'已成交，请重新选择']);
        }
        $res = Db::table('up_house')
            ->where('h_id',$h_id)
            ->update([
                'h_done' => 2,
                'h_status' => 2,
                'h_done_time' => date('Y-m-d H:i:s')
            ]);
        if($res){
            Db::table('up_u_user')->where('user_id',$have['h_user_id'])->setInc('user_chengjiao');
            return json(['status'=>200,'msg'=>'ok']);
        }else{
            return json(['status'=>100,'msg'=>'状态更改失败，请刷新重试']);
        }
    }
    /**
     * 删除房源
     */
    public function deleteMyHouse()
    {
        extract(input());
        $data = Db::table('up_house')
            ->where('h_id',$h_id)
            ->delete();
        if($data){
            return json(['status'=>200,'msg'=>'删除成功']);
        }else{
            return json(['status'=>100,'msg'=>'删除失败']);
        }
    }
    //浏览记录
    public function browse(){
        $data = Db::name('history')->alias('l')
            ->join('up_house uh','uh.h_id = l.his_h_id','left')
            ->join('up_u_user uuu','uuu.user_id = uh.h_user_id','left')
            ->field('uh.*,uuu.user_phone')
            ->order('l.his_user_id desc')
            ->where('his_user_id',session('user_id'))
            ->where('h_status',1)
            ->where('h_id','>',0)
            ->paginate(10);
        $page = $data->render();
        $data = $data->all();
        foreach ($data as $key=>&$vo){
            $sign = trim($vo['h_sign'],',');
            $where['id'] = array('IN',$sign);
            $name = Db::table('up_sign')->where($where)->column('name');
            $vo['sign_name'] = $name;
            $vo['price'] = floor($vo['h_price']);
        }
        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('title','浏览记录');
        return $this->fetch();
    }

    //认证
    public function attestation(){
        if(request()->isPost ()){
            extract(input());
            $task_image = input('param.task_image');
            $task_image1 = input('param.task_image1');
            $task_image2 = input('param.task_image2');
            $task_image3 = input('param.task_image3');
            $com_name = input('param.com_name');
            if($task_image == '' || $task_image1 == '' || $task_image2 == '' || $task_image3 == '' || $com_name == ''){
                return json(['code'=>100,'msg'=>'有未填项']);
            }
            $param = [
              'user_zheng'=>$task_image,
              'user_fan'=>$task_image1,
              'user_card'=>$task_image2,
              'user_zhizhao'=>$task_image3,
              'user_shop'=>$com_name,
              'user_reject_time'=>date('Y-m-d H:i:s'),
                'user_status'=>0
            ];
            $user = Db::name('u_user')->where('user_id',session('user_id'))
                ->field('user_type,user_status')->find();
            if($user['user_type'] == 3){
                $update['user_shop'] = Db::table('up_company')->where('id',$com_name)->value('name');
                $update['user_company_id_3'] = $com_name;
            }
            $res = Db::name('u_user')->where('user_id',session('user_id'))->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'提交成功']);
            }else{
                return json(['code'=>100,'msg'=>'提交失败']);
            }
        }
        $data = Db::name('u_user')->where('user_id',session('user_id'))->find();
        $this->assign('title','我的认证');
        $this->assign('data',$data);
//
        $coms = Db::table('up_company')
            ->order('tuijian DESC,time DESC')
            ->field('name,title,address,id')
            ->select();
        $this->assign('coms',$coms);
//
        return $this->fetch();
    }
    //充值
    public function recharge(){
        $user = Db::table('up_u_user')
            ->where('user_id',session('user_id'))
            ->field('user_id,user_mytop,user_myfresh,user_auth')
            ->find ();
        $jilu =  Db::table('up_order')
            ->where('order_user_id',session('user_id'))
            ->where('order_status',2)
            ->where('order_type','<>',1)
            ->field(['order_date','order_type','order_money'])
            ->order('order_time DESC')
            ->paginate(10);
        $this->assign('title','充值');
        $this->assign('user',$user);
        $this->assign('jilu',$jilu);
        return $this->fetch();
    }
    //会员
    public function member(){
        $user = Db::table('up_u_user')
            ->where('user_id',session('user_id'))
            ->field('user_id,user_mytop,user_myfresh,user_auth,user_auth_start,user_auth_end,user_top_use,user_fresh_use')
            ->find ();
        $config = Db::table('up_member_config')
            ->where('mc_auth',$user['user_auth'])
            ->find();
        $data = Db::name('house h')
            ->join ('up_area a','h.h_shi = a.district_id','LEFT')
            ->where('h_user_id',session('user_id'))
            ->where('h_status',1)
            ->field('h_id,h_title,h_sign,h_xiaoqu,h_price,h_shi,h_type,h_shis,h_tings,h_mianji,h_img,h_weis,district,h_use_top_status,h_use_fre_status,h_use_top_time,h_use_fre_time,h_video canfresh,h_remark cantop')
            ->order('h_time DESC')
            ->paginate(5)
            ->each(function($item, $key){
                $today = date('Y-m-d H:i:s');
                $minute_30 = date("Y-m-d H:i:s",(strtotime("{$today}")-60*30));
                $item['canfresh'] = 1;
                $item['cantop'] = 1;
                if($item['h_use_fre_time'] && $item['h_use_fre_time'] > $minute_30){
                    $item['canfresh'] = 0;
                }
                if($item['h_use_top_time'] && $item['h_use_top_time'] > $minute_30){
                    $item['cantop'] = 0;
                }
                return $item;
            });
        $types = [1=>'免税', 2=>'精装',3=>'学区房',4=>'配套完善',5=>'低首付',6=>'方便停车',7=>'环境优美',
            8=>'急售',9=>'婚房',10=>'短租', 11=>'拎包入住',12=>'精装修',13=>'配套完善',
            14=>'可供暖',15=>'停车方便'];
        $this->assign('title','会员');
        $this->assign('user',$user);
        $this->assign('types',$types);
        $this->assign('data',$data);
        $this->assign('config',$config);
        $authstrs = ['普通会员','普通会员','季度会员','半年会员','一年会员'];
        $this->assign('authstrs',$authstrs);
        if($user['user_auth'] == 1){
            return $this->fetch('user/member_none');
        }else{
            return $this->fetch('user/member_have');
        }

    }
    /**
     * 使用置顶包或者刷新包
     * type：1是使用置顶，2是使用刷新
     */
    public function useBagInHome()
    {
        extract(input());
        $user_id = session('user_id');
        $apibase = new \app\api\controller\Base();
        $config = $apibase->getMemberConfig($user_id);
        $user = Db::table('up_u_user')
            ->where('user_id',$user_id)
            ->field('user_mytop,user_myfresh,user_top_use,user_fresh_use,user_type')
            ->find ();
        $h = Db::table('up_house')->where('h_id',$h_id)->field('h_type,h_shi')->find();
        $h_type = $h['h_type'];
        $h_shi = $h['h_shi'];
        if($type == 1){
            if($user['user_mytop'] <= 0 && $config['mc_top'] <= $user['user_top_use']){
                return json(['status'=>1,'msg'=>'置顶包数量不足，请前去购买']);
            }else{

                Db::startTrans();
                try {
                    if($config['mc_top'] > $user['user_top_use']){
                        Db::table('up_u_user')->where('user_id', $user_id)->setInc('user_top_use');
                    }else{
                        Db::table('up_u_user')->where('user_id', $user_id)->setDec('user_mytop');
                    }
                    $data = array();
                    $data['top_h_id'] = $h_id;
                    $data['top_type'] = $h_type;
                    $data['top_time'] = date('Y-m-d H:i:s');
                    $data['top_updatetime'] = date('Y-m-d H:i:s');
                    $data['top_updatetime2'] = date('Y-m-d H:i:s');
                    $data['top_shi'] = $h_shi;
                    Db::table('up_top')->where('user_id', $user_id)->insert($data);
                    Db::table('up_house')->where('h_id', $h_id)
                        ->update(['h_use_top_status'=>2,'h_use_top_time'=>date('Y-m-d H:i:s')]);
                    //PS：如果是经纪人 改一下房源表的list时间和order-----start
                    if($user['user_type'] == 2){
                        Db::table('up_house')->where('h_id', $h_id)
                            ->update(['h_listtime'=>date('Y-m-d H:i:s'),'h_order'=>2]);
                        $apibase->updateHouseList($h_type,$h_shi);
                    }
                    //PS：如果是经纪人 改一下房源表的list时间和order----- end
                    Db::commit();
                    return json(['status'=>200,'msg'=>'操作成功']);
                } catch (\Exception $e) {
                    Db::rollback();
                    return json(['status'=>0,'msg'=>'操作失败']);
                }
            }
        }else{
            if($user['user_myfresh'] <= 0 && $config['mc_fresh'] <= $user['user_fresh_use']){
                return json(['status'=>1,'msg'=>'刷新包数量不足，请前去购买']);
            }else{
                Db::startTrans();
                try {
                    if($config['mc_fresh'] > $user['user_fresh_use']){
                        Db::table('up_u_user')->where('user_id', $user_id)->setInc('user_fresh_use');
                    }else{
                        Db::table('up_u_user')->where('user_id', $user_id)->setDec('user_myfresh');
                    }
                    $data = array();
                    $data['h_time'] = date('Y-m-d H:i:s');
                    $data['h_listtime'] = date('Y-m-d H:i:s');
                    $data['h_use_fre_status'] = 2;
                    $data['h_use_fre_time'] = date('Y-m-d H:i:s');
                    Db::table('up_house')->where('h_id', $h_id)->update($data);
                    Db::commit();
                    return json(['status'=>200,'msg'=>'操作成功']);
                } catch (\Exception $e) {
                    Db::rollback();
                    return json(['status'=>0,'msg'=>'操作失败']);
                }
            }
        }
    }
    //选择买会员
    public function vip_top(){
        $config = Db::table('up_member_config')->where('mc_auth','>',1)->select();
        $config2 = Db::table('up_member_price')->where('mp_phone',1)->select();
        $this->assign('config',$config);
        $this->assign('config2',$config2);
        return $this->fetch();
    }
    //去买会员
    public function buyauth(){
        extract(input());
        $money = Db::table('up_member_price')
            ->where('mp_auth',$type)
            ->where('mp_phone',1)
            ->value('mp_price');
        $this->assign('type',$type);
        $this->assign('money',$money);
        return $this->fetch();
    }
    //去买置顶包或者刷新包
    public function buytopfresh(){
        extract(input());
        $money = Db::table('up_top_bag')
            ->where('t_b_id',$which-1)
            ->value('t_b_price');
        $this->assign('which',$which);
        $this->assign('money',$money);
        return $this->fetch();
    }
}